﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Web.Hosting;
using System.Web.UI.WebControls;

namespace VideoNetApplication.Common.Manage
{
    public class MenuDao
    {
        private string _XMLpath;
        private static MenuDao dal = null;
        private MenuDao()
        {
            InitXMLpath();
        }

        public static MenuDao Current
        {
            get
            {
                if (dal == null)
                    dal = new MenuDao();

                return dal;
            }
        }

        private void InitXMLpath()
        {
            string xmlpath = "~/app_data/xmlmenus.xml";
            _XMLpath = HostingEnvironment.MapPath(xmlpath);
            FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Write, _XMLpath);
            permission.Demand();
        }

        private DataTable GetAllMenus()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(_XMLpath);
            return ds.Tables[0];
        }

        private DataTable GetAllMenus(int parentid)
        {
            DataTable dt = GetAllMenus();
            DataTable _dt = dt.Clone();
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["parentid"].ToString() == parentid.ToString())
                    _dt.Rows.Add(dr.ItemArray);
            }

            return _dt;
        }

        public string CreateHTML()
        {
            StringBuilder sb = new StringBuilder();
            DataTable dt = GetAllMenus(0);
            DataTable dt2 = null;
            string _tempHtml;
            foreach (DataRow dr in dt.Rows)
            {
                sb.Append("{title:'" + dr["title"].ToString() + "',autoScroll:true,border:false,iconCls:'nav',");

                dt2 = GetAllMenus(int.Parse(dr["id"].ToString()));
                if (dt2.Rows.Count > 0)
                {
                    _tempHtml = "<ul class=\"LeftNav\">";
                    foreach (DataRow dr2 in dt2.Rows)
                    {
                        _tempHtml += "<li><a target=\"main\" href=\"" + dr2["url"].ToString() + "\"> " + dr2["title"].ToString() + "</a></li>";
                    }

                    if (_tempHtml != "<ul>")
                    {
                        _tempHtml += "</ul>";
                        sb.Append("html:'" + _tempHtml + "'}");
                    }
                    else
                    {
                        sb.Append("html:''}");
                    }
                }
                else
                {
                    sb.Append("html:''}");
                }

                sb.Append(",");
            }

            return sb.ToString().TrimEnd(',');
        }

        public void BoundTree(TreeNodeCollection nodes)
        {
            DataTable dt = GetAllMenus(0);
            TreeNode tn = null;
            DataTable _dt = null;
            TreeNode _tn = null;
            foreach (DataRow dr in dt.Rows)
            {
                tn = new TreeNode();
                tn.Text = dr["title"].ToString();
                tn.Value = dr["id"].ToString();
                //tn.NavigateUrl = "";
                //tn.Target = "main";
                tn.SelectAction = TreeNodeSelectAction.SelectExpand;

                nodes.Add(tn);

                _dt = GetAllMenus(int.Parse(dr["id"].ToString()));
                if (_dt.Rows.Count > 0)
                {
                    foreach (DataRow _dr in _dt.Rows)
                    {
                        _tn = new TreeNode();
                        _tn.Text = _dr["title"].ToString();
                        _tn.NavigateUrl = _dr["url"].ToString();
                        _tn.Target = "main";
                        _tn.Value = _dr["id"].ToString();
                        _tn.SelectAction = TreeNodeSelectAction.SelectExpand;
                        tn.ChildNodes.Add(_tn);
                    }
                }
            }
        }
    }
}
